<?php

/**
 * This is the model class for table "online_news".
 *
 * The followings are the available columns in table 'online_news':
 * @property integer $id
 * @property string $title
 * @property string $title_md5
 * @property string $avatar
 * @property string $summary
 * @property string $html_content
 * @property string $content
 * @property string $datetime
 * @property integer $online_news_category_id
 * @property integer $web_id
 * @property string $vendor_link
 * @property string $vendor_link_md5
 * @property integer $has_main_image
 * @property string $keywords
 * @property integer $sync
 * @property integer $time
 */
class OnlineNews extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'online_news';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('online_news_category_id, web_id, has_main_image, sync, time', 'numerical', 'integerOnly'=>true),
			array('title, vendor_link', 'length', 'max'=>1500),
         array('title_md5, vendor_link_md5, avatar', 'length', 'max'=>255),
			array('summary', 'length', 'max'=>3000),
			array('keywords', 'length', 'max'=>1000),
			array('html_content, content, datetime', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, title, title_md5, avatar, summary, html_content, content, datetime, online_news_category_id, web_id, vendor_link, vendor_link_md5, has_main_image, keywords, sync, time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'title' => 'Title',
         'title_md5' => 'Title Md5',
         'avatar' => 'Avatar',
			'summary' => 'Tóm tắt',
			'html_content' => 'Html Content',
			'content' => 'Nội dung',
			'datetime' => 'Ngày tháng',
			'online_news_category_id' => 'Danh mục',
         'web_id' => 'Web',
			'vendor_link' => 'Vendor Link',
         'vendor_link_md5' => 'Vendor Link Md5',
			'has_main_image' => 'Has Main Image',
			'keywords' => 'Keywords',
			'sync' => 'Đồng bộ',
         'time' => 'Time',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('title',$this->title,true);
      $criteria->compare('title_md5',$this->title_md5,true);
      $criteria->compare('avatar',$this->avatar,true);
		$criteria->compare('summary',$this->summary,true);
		$criteria->compare('html_content',$this->html_content,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('datetime',$this->datetime,true);
		$criteria->compare('online_news_category_id',$this->online_news_category_id);
      $criteria->compare('web_id',$this->web_id);
		$criteria->compare('vendor_link',$this->vendor_link,true);
      $criteria->compare('vendor_link_md5',$this->vendor_link_md5,true);
		$criteria->compare('has_main_image',$this->has_main_image);
		$criteria->compare('keywords',$this->keywords,true);
		$criteria->compare('sync',$this->sync);
      $criteria->addCondition('sync = 0');
      $criteria->compare('time',$this->time);
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
         'sort'=>array(
             'defaultOrder'=>'id DESC',
         ),
         'pagination'=>array(
            'pageSize'=> Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']),
         ),
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return OnlineNews the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
   
   /**
    * Get full path image 
    * @author Sondv
   */
   public function getImageFullSize($imgName){
      $imgServer  = "/";
      return $imgServer . 'pictures/picfullsizes/'.$this->getPathDateTimeImg($imgName);
   }
   
   
   public function getImageSmall($sourceFilename, $maxWidth = 0,$maxHeight = 0){
      $imgServer  = "/";
      global $pathThemes;
      if($maxHeight == 0){
      $maxHeight = 1000;
      }
      if($maxWidth < 50) $maxWidth = 50;
      if($maxHeight < 100&&$maxHeight>0) $maxHeight = 100;
      
      if($sourceFilename == "") return $pathThemes . "/images/default.png";
      return  $imgServer. "pictures/picsmalls/" . getPathDateTimeImg($sourceFilename,"",$maxWidth);
   }
   
   public function getPathDateTimeImg($filename, $pre_path = "",$width_height =""){
      $explode = explode("/", $filename);
      $filename = end($explode);
      
      $time = intval(preg_replace("/[^0-9]/i","",$filename));
      if($time > time()) $time = time();
      return $pre_path . date("Y/m/d/", $time) . (($width_height != "") ? $width_height . '/' : '') . $filename; 
   }
}
